/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.utility;

import java.util.Vector;

/**
 * This class can be used in replacing special characters in XML documents, i.e.
 * & should be replaced by &amp;, < should be replace by &lt; and > should be replaced
 * by &gt;. If these characters are not replaced, they will be send to the server that
 * tries to parse the XML document. However, since the document eventually will 
 * contain these special characters, there will appears errors. 
 */
public class StringUtils {
	
	public static String replace(String text, String searchStr, String replacementStr) {
		StringBuffer sb = new StringBuffer();
        int searchStringPos = text.indexOf(searchStr);
        int startPos = 0;
        int searchStringLength = searchStr.length();
        while (searchStringPos != -1) {
            sb.append(text.substring(startPos, searchStringPos)).append(replacementStr);
            startPos = searchStringPos + searchStringLength;
            searchStringPos = text.indexOf(searchStr, startPos);
        }
        sb.append(text.substring(startPos, text.length()));
        return sb.toString();
    }
	
	/**
	 * Utility method for checking whether a String is null or empty
	 * @param s
	 * @return
	 */
	public static boolean isNullOrEmpty(String s) {
		if (s == null)
			return true;

		if (s.equals(""))
			return true;

		return false;
	}
	
	/**
	 * Implementation of the split method for J2ME
	 * @param splitStr
	 * @param delimiter
	 * @return
	 */
	public static String[] split(String splitStr, char delimiter) {  
		StringBuffer buffer = new StringBuffer();
		
		Vector tokens = new Vector();
		
		char[] chars = splitStr.toCharArray();
		for(int i=0; i< chars.length; i++){
			if(delimiter == chars[i]){
				if(buffer.length()>0){
					tokens.addElement(buffer.toString());
					buffer.setLength(0);
				}
			}else{
				buffer.append(chars[i]);
			}
		}
		
		//the tail
		if(buffer.length() > 0)
			tokens.addElement(buffer.toString());
		
		//convert the vector in an array
		String[] splitArray = new String[tokens.size()];
		for(int i=0; i< splitArray.length; i++)
			splitArray[i] = tokens.elementAt(i).toString();
		
		return splitArray;	
	}  
}
